package com.gdth.bank.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gdth.bank.R;
import com.gdth.database.bean.TAppBfrjBfjl;
import com.gdth.model.Item;
import com.gdth.utils.DateTimeUtil;

/**
 * 拜访记录
 * @author arvin
 *
 */
public class CjkhBfjlListViewAdapter extends BaseAdapter{
	private Context context;

	private List<TAppBfrjBfjl> vList;
	
	private int layout;
	
	private List<Item> bflxList;
	
	public CjkhBfjlListViewAdapter(Context context, List<TAppBfrjBfjl> vList) {  
	    this.context = context;
	    this.vList = vList;
	    
	    this.layout = R.layout.item_cjkh_bfrj;
	}
	
	@Override 
	public int getCount() {  
	    return vList.size();  
	}  
	
	@Override 
	public Object getItem(int position) {  
	    return position;  
	}  
	
	@Override 
	public long getItemId(int position) {  
	    return position;  
	}
	
	@Override 
	public View getView(final int position, View convertView, ViewGroup parent) { 
		TAppBfrjBfjl model = vList.get(position);
		ViewHolder holder = null;
		if (convertView == null){
			holder = new ViewHolder();
			
			convertView = LayoutInflater.from(context).inflate(layout, null);
			
		    holder.local_bflx = (TextView) convertView.findViewById(R.id.bfrj_item_bflx);
		    holder.local_bfrq = (TextView) convertView.findViewById(R.id.bfrj_item_bfrq);
			holder.local_del = (TextView) convertView.findViewById(R.id.bfrj_item_detail);

			convertView.setTag(holder);
	    }
		
		else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if (holder != null){
			if (bflxList != null) {
				for (Item item : bflxList) {
					if (item.getKey().equals(model.getBflx().toString())) {
						holder.local_bflx.setText(item.getValue());
						break;
					}
				}
			}
			holder.local_bfrq.setText(DateTimeUtil.getCurrentTime(model.getCjsj(), "yyyy-MM-dd HH:mm:ss"));
		}
		if(this.mOnBfjlDetailClickListener !=null && DateTimeUtil.isToday(model.getCjsj())){
			holder.local_del.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mOnBfjlDetailClickListener.clickListener(position);
				}
			});
		}else{
			holder.local_del.setVisibility(View.INVISIBLE);
		}

		
	    return convertView;  
	}
	
	public void setBflx(List<Item> bflxList) {
		this.bflxList = bflxList;
	}
	
	public final class ViewHolder{
		public TextView local_bflx = null;
		public TextView local_bfrq = null;
		public TextView local_del = null;
	}

	private CjkhBfjlListViewAdapter.OnbfjlDetailClickListener mOnBfjlDetailClickListener;

	public void setmOnJtcyXxDetailClickListener(
			CjkhBfjlListViewAdapter.OnbfjlDetailClickListener mOnBfjlDetailClickListener) {
		this.mOnBfjlDetailClickListener = mOnBfjlDetailClickListener;
	}

	public interface OnbfjlDetailClickListener {
		void clickListener(int position);

	}
}
